import {Buffer} from "buffer";
import {DataUtils} from "./DataUtils";

export class ReverseByteArrayOutputStream {

    buffer: Buffer | null = null;
    index: number = 0;
    automaticResize: boolean = false;
    constructorIndex: number = -1;

    constructor() {
    }

    setBufSize(bufferSize: number) {
        this.buffer = Buffer.alloc(bufferSize);
        this.index = bufferSize - 1;
        this.automaticResize = false;
    }

    setBufSize_Resize(bufferSize: number, automaticResize: boolean) {
        this.buffer = Buffer.alloc(bufferSize);
        this.index = bufferSize - 1;
        this.automaticResize = automaticResize;
    }

    setBuf(buffer: Buffer) {
        this.buffer = buffer;
        this.index = buffer.length - 1;
        this.automaticResize = false;
    }

    setBuf_Index(buffer: Buffer, index: number) {
        this.buffer = buffer;
        this.index = index;
        this.automaticResize = false;
    }

    setBuf_Index_Resize(buffer: Buffer, index: number, automaticResize: boolean) {
        this.buffer = buffer;
        this.index = index;
        this.automaticResize = automaticResize;
    }

    writeByte(byteArray: Buffer) {
        if (this.buffer == null) {
            throw new Error("buffer is null");
        } else {
            console.log("byteArray.length " + byteArray.length);
            // console.log(this.buffer[-1]);
            for (let i = byteArray.length - 1; i >= 0; i--) {

                if(this.index == -1){
                    if (this.automaticResize) {
                        this.resize();
                        this.buffer[this.index] = byteArray[i];
                    } else {
                        throw new Error("buffer.length = " + this.buffer.length);
                    }
                }else{
                    console.log(this.buffer.length + " " + this.index);
                    this.buffer[this.index] = byteArray[i];
                }
                this.index--;
            }
        }
    }

    write(data: number) {
        if (this.buffer == null) {
            throw new Error("buffer is null");
        } else {
            if(this.index == -1){
                console.log("this.index" + this.index);
                if (this.automaticResize) {
                    this.resize();
                    this.buffer[this.index] = data;
                } else {
                    throw new Error("buffer.length = " + this.buffer.length);
                }
            }else{
                this.buffer[this.index] = data;
                console.log("this.index" + this.index);
            }
            this.index--;
        }
    }

    resize() {
        if (this.buffer == null) {
            throw new Error("buffer is null");
        } else {
            let newBuf = Buffer.alloc(this.buffer.length * 2);
            this.buffer.copy(newBuf, this.buffer.length + this.index + 1, this.index + 1, this.index + 1 + this.buffer.length - this.index - 1)
            console.log("resize " + (this.buffer.length + this.index + 1) + " " + (this.index + 1) + " " + (this.index + 1 + this.buffer.length - this.index - 1));
            this.index += this.buffer.length;
            this.buffer = newBuf;
        }
    }

    getArray(): Buffer {
        if (this.buffer == null) {
            throw new Error("buffer is null");
        } else {
            if (this.index == -1) {
                return this.buffer;
            }
            let subBufferLength = this.buffer.length - this.index - 1;
            let subBuffer = Buffer.alloc(subBufferLength);
            this.buffer.copy(subBuffer, 0, this.index + 1, this.index + 1 + subBufferLength);
            return subBuffer;
        }
    }

    reset() {
        if (this.buffer == null) {
            throw new Error("buffer is null");
        } else {
            this.index = this.buffer.length - 1;
        }
    }

}



